<?php
namespace app\hsycms\model;
use think\Db;
use think\Model;

/**
 * 文章Tag标签
 */
class Taglist extends Model
{	
  
	//添加TAG标签
	public function addTag($aid="",$nid="",$tags="",$act="add",$oldtags=""){
		if($act=="add"){
			if($tags!=""){
				$tagsList = explode('|',$tags);				//分割成数组				
				foreach($tagsList as $key=>$v){
					$v = trim($v);					
					$tagres = db('taglist')->where(['tag'=>$v])->find();
					if(!$tagres){
						$tid = db('taglist')->insertGetId(['tag'=>$v,'datetime'=>time()]);
						db('tagnidaid')->insert(['tid'=>$tid,'nid'=>$nid,'aid'=>$aid]);
					}else{
						$mp['tid'] = db('taglist')->where(['tag'=>$v])->value('id');
						$mp['nid'] = $nid;
						$mp['aid'] = $aid;
						if(!db('tagnidaid')->where($mp)->find()){
							db('tagnidaid')->insert(['tid'=>$mp['tid'],'nid'=>$nid,'aid'=>$aid]);
						}
					}
				}
			}
		}else{
			//旧TAG不为空
			if($oldtags != ""){ 
				$tagsList = explode('|',$oldtags);		//分割成数组				
				foreach($tagsList as $key=>$v){
					$v = trim($v);
					$tid = db('taglist')->where(['tag'=>$v])->value('id');
					$res = db('tagnidaid')->where(['tid'=>$tid,'nid'=>$nid,'aid'=>$aid])->find();
					if($res){
						db('tagnidaid')->where(['tid'=>$tid,'nid'=>$nid,'aid'=>$aid])->delete();
						if(db('tagnidaid')->where(['tid'=>$tid])->count()==0){
							db('taglist')->where(['tag'=>$v])->delete();
						}
					}
				}
			}
			//新TAG不为空 
			if($tags != ""){ 
				$this->addTag($aid,$nid,$tags,$act="add",$oldtags="");
			}			
		}
	}
	
	//删除TAG并更新文章TAG
	public function delTag($tid=""){
		$tag = db('taglist')->where(['id'=>$tid])->value('tag');
		$arclist = db('tagnidaid')->where(['tid'=>$tid])->select();
		foreach($arclist as $key=>$v){
			$tags = db('article')->where(['id'=>$v['aid']])->value('tags');
			if($tags!=""){
				$tagarr = explode('|',$tags);
				foreach($tagarr as $k=>$vs){
					if($vs==$tag){ 
						unset($tagarr[$k]); 
					}
				}
				$newtag = implode('|',$tagarr);
				db('article')->where(['id'=>$v['aid']])->update(['tags'=>$newtag]);
			}
		}
		db('tagnidaid')->where(['tid'=>$tid])->delete();
	}
	
	//文章删除同时删除TAG
	public function delArcTag($aid=""){
		 $res = db('article')->where(['id'=>$aid])->find();
		 if($res['tags']!=""){
			 $this->addTag($aid,$res['nid'],"",$act="edit",$res['tags']);
		 }
	}
	
	//修改文章TAG
	public function editArcTag($tid="",$newtag="",$oldtag=""){
		$arclist = db('tagnidaid')->where(['tid'=>$tid])->select();
		foreach($arclist as $key=>$v){
			$tags = db('article')->where(['id'=>$v['aid']])->value('tags');
			if($tags!=""){
				$tags = str_replace($oldtag,$newtag,$tags);
				db('article')->where(['id'=>$v['aid']])->update(['tags'=>$tags]);
			}
		}
	}
	
	//关联文章TAG
	public function glArcTag($aid="",$tid="",$tag=""){
		$res = db('article')->field('nid,tags')->where(['id'=>$aid])->find();		
		if($res['tags']!=""){
			$tag = $tag.'|'.$res['tags'];
		}
		db('article')->where(['id'=>$aid])->update(['tags'=>$tag]);
		$d['tid'] = $tid;
		$d['nid'] = $res['nid'];
		$d['aid'] = $aid;
		if(!db('tagnidaid')->where($d)->find()){
			db('tagnidaid')->insert($d);
		}
	}
	
}